home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: mcorcora@ix.netcom.com(Marian Corcoran )
- Newsgroups: comp.lang.c++
- Subject: RE: Function body banned in .H file - Can I still inline?
- Date: 14 Jan 1996 22:42:11 GMT
- Organization: Netcom
- Message-ID: <4dc0s3$dlo@ixnews7.ix.netcom.com>
- References: <4d0ir0$68e@newsroom.hitc.com> <00001a81+00008dc1@msn.com>
- NNTP-Posting-Host: ix-sj35-03.ix.netcom.com
- X-NETCOM-Date: Sun Jan 14 2:42:11 PM PST 1996
-
- In <00001a81+00008dc1@msn.com> SimsGW@msn.com (Gary Sims) writes:
- >
- >I think you've misunderstood something you read, Chris. You can put
- >the body of a function in the header file. That's the only practical
- >way to have inline functions. What you can NOT do is present the body
- >to the compiler more than once. Since the same header is frequently
- >#include'd by one more than one module, you must prevent it being
- >interpreted twice. Here's how you do that. I've included an example
- >of the two ways to specify an inline function:
- >
- >#ifndef THIS_HEADER_NAME
- >#define THIS_HEADER_NAME
- >
- >class Foo
- >{
- > int PrivateXValue;
- >public:
- > Foo();
- > int GetX(){return PrivateXValue;};
- > int GetFractionalX(const int Divisor);
- >};
- >
- >int inline Foo::GetFractionalX(const int Divisor)
- >{
- > return PrivateXValue/Divisor;
- >};
- >
- >#endif
- >
- >The first time the compiler sees this, the value THIS_HEADER_NAME
- >will not have been defined, so the lines between there and the #endif
- >will be processed. On successive occasions, it WILL be defined, so
- >they won't. That avoids the problem of double definitions of the
- >function body. Hope this helps,
- >
- >Gary W. Sims
- >Stonehaven Laboratory
-
- There is also another technique to speed things up. Say the name
- of the file is header1.h. You can use a conditional in the
- source where you are going to do the #include
-
- #ifndef __HEADER1_H__
- #include "header1.h"
- #endif
-
- That will save you the time of going out to see if the header
- has been included. There is some disagreement about whether
- this is a good idea. One guide says it is error-prone another
- guide recommends it. Of course, you would still put the
- #ifndef ... as originally suggested above in the header file itself.
-
-
- Marian
-
-